import { getSimcardDetail } from '../services/api';

export default {
  namespace: 'simcardDetail',

  state: {
    data: [],
  },
  effects: {
    *fetchSimCardDetail({ payload }, { call, put }) {
      const rsp = yield call(getSimcardDetail, {
        iccid: payload.iccid,
      });
      yield put({
        type: 'setData',
        payload: rsp.data.map(detail => ({
          date: detail.date.slice(11, 13),
          usage: Number(detail.ctdDataUsage / (1024 * 1024)).toFixed(3),
        })),
      });
    },
  },
  reducers: {
    setData(state, action) {
      return {
        ...state,
        data: action.payload,
      };
    },
  },
};
